package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.EduExper;
import com.chixing.ih.module.programmer.service.IEduExperService;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.util.RequestIdUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/3
 * Time: 9:08
 */
@RestController
@RequestMapping("/eduExper")
@Api(tags = "程序员模块教育经历相关Api")
public class EduExperController {

    @Autowired
    IEduExperService eduExperService;

    @Autowired
    IProgrammerService programmerService;

    /**
     * desc:批量保存教育经历接口
     *
     * @param eduExpers
     * @return
     */
    @ApiOperation(value = "批量保存教育经历接口", notes = "批量保存教育经历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教育经历id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "beginTime", value = "教育开始时间", dataType = "date"),
            @ApiImplicitParam(name = "endTime", value = "教育结束时间", dataType = "date"),
            @ApiImplicitParam(name = "schoolName", value = "学校名称", dataType = "string"),
            @ApiImplicitParam(name = "perfessionName", value = "专业名称", dataType = "string"),
            @ApiImplicitParam(name = "category", value = "学历", dataType = "string"),
            @ApiImplicitParam(name = "awardsDesc", value = "获奖描述", dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败")
    })
    @PutMapping("/eduExperList")
    public Map<String, Object> saveListEduExpers(@RequestBody List<EduExper> eduExpers, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = eduExperService.updateList(eduExpers);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();


    }

    /**
     * desc:查询教育经历接口
     *
     * @return
     */
    @ApiOperation(value = "查询教育经历接口", notes = "根据用户id添查询教育经历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "程序员id", required = true, dataType = "long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),

    })

    @GetMapping("/eduExperList/{userId}")
    public Map<String, Object> listEduExpers(@PathVariable("userId") long userId, HttpServletRequest request) {
        List<EduExper> eduExpers = eduExperService.listAll(userId);
        return ResponseData.REP_GET_SUCCESS(eduExpers);

    }

    /**
     * desc:获取新增教育经历id接口
     *
     * @return
     */
    @ApiOperation(value = "获取新教育经历id接口", notes = "获取新教育经历id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "添加失败")
    })

    @PostMapping("/eduExper")
    public Map<String, Object> saveEduExper(HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        EduExper eduExper = new EduExper();
        eduExper.setUserId(myId);
        EduExper eduExper1 = eduExperService.saveList(eduExper);
        if (eduExper1 != null) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_GET_SUCCESS(eduExper1);
        } else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:删除一条教育经历接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一条教育经历接口", notes = "删除一条教育经历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教育经历id", required = true, dataType = "long"),
    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "删除成功"),
            @ApiResponse(code = 400, message = "删除失败")
    })

    @DeleteMapping("/eduExper/{id}")
    public Map<String, Object> deleteEduExper(@PathVariable("id") long id, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = eduExperService.deleteEduExper(id);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();

    }


}
